package com.jianyun.jyzs.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.EDMessageActivity;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.bean.MessageData;
import com.jianyun.jyzs.bean.PDDetial;
import com.jianyun.jyzs.bean.TimeEngineer;
import com.jianyun.jyzs.dao.EnginnerMsgDao;
import com.jianyun.jyzs.utils.LocalSp;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.widget.CicleImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by mlli on 2017/11/11.
 * 个人动态
 */

public class PDAdapter extends RecyclerView.Adapter {


    private  String userId;
    private Activity context;
    private List<TimeEngineer> list;
    private final Gson gson;
    private String username;
    private String headPath;
    private String UserId= LoginCache.getInstance().getUserInfo().getUserId();
    public PDAdapter(Activity context, List<TimeEngineer> list,String userid) {
        this.context = context;
        this.list = list;
        gson = new Gson();
        this.userId=userid;
    }

    public void setList(List<TimeEngineer> list, int i) {
        this.list = list;
        notifyDataSetChanged();
    }
    public void setUserName(String username){
        this.username=username;
    }

    public void setHeadImgPath(String path){
        this.headPath=path;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == 0) {
            view = LayoutInflater.from(context).inflate(R.layout.personal_top, parent, false);
            return new HeadHolder(view);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.list_personal_dynamic, parent, false);
            return new MHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        TimeEngineer engineer = list.get(position);
        return engineer.getCategory();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        TimeEngineer engineer = list.get(position);
        int category = engineer.getCategory();
        if (category == 0) {
            setHeadView((HeadHolder) holder, engineer);
        } else {
            List<EngineeringResult.EngineerBean> engineerList = engineer.getEngineerList();
            MHolder mHolder = (MHolder) holder;
            mHolder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
            PdItemAdaper adaper = new PdItemAdaper(context, engineerList);
            mHolder.recyclerView.setAdapter(adaper);
        }

    }


    //设置头部数据
    private void setHeadView(final HeadHolder holder, TimeEngineer engineer) {
        PDDetial pdDetial = engineer.getPdDetial();
        holder.tvPlaceCount.setText(pdDetial.getPart() + "");
        holder.tvUpCount.setText(pdDetial.getUploadCount() + "");
        holder.llMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //进入消息列表
                context.startActivity(new Intent(context, EDMessageActivity.class));
            }
        });

        if (!this.userId.equals(pdDetial.getUserId())) {
            holder.llStat.setVisibility(View.INVISIBLE);
        } else {
            List<MessageData> enginmsgByUserId = new EnginnerMsgDao(context).getEnginmsgByUserId(userId);
            //int size = LocalSp.getInstance().getEdMessageList().size();
            holder.tvMsgCount.setText(enginmsgByUserId.size() + "");
        }
        if (LocalSp.getInstance().getEdNewMsgTag(UserId)) {
            holder.ivUnread.setVisibility(View.VISIBLE);
        } else {
            holder.ivUnread.setVisibility(View.INVISIBLE);
        }
        holder.tvName.setText(username);

        if (headPath!=null){
            Picasso.with(context).load(headPath).into(holder.ivHead);
        }

    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;

        public MHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class HeadHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_head)
        CicleImageView ivHead;
        @BindView(R.id.tv_placeCount)
        TextView tvPlaceCount;
        @BindView(R.id.tv_upCount)
        TextView tvUpCount;
        @BindView(R.id.iv_unread)
        ImageView ivUnread;
        @BindView(R.id.ll_msgCount)
        LinearLayout llMsg;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.ll_statData)
        LinearLayout llStat;
        @BindView(R.id.tv_msgCount)
        TextView tvMsgCount;

        public HeadHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

}
